package com.hzp.lolnews.ui.me.view;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hzp.lolnews.R;
import com.hzp.lolnews.activity.view.CollectionActivity;
import com.hzp.lolnews.activity.view.LoginActivity;
import com.hzp.lolnews.base.BaseFragment;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

/**
 * A simple {@link Fragment} subclass.
 */
public class MeFragment extends BaseFragment implements View.OnClickListener {

    private UMShareAPI mShareAPI;
    private TextView nickName;
    private ImageView avator;
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LinearLayout tvLogin = (LinearLayout) view.findViewById(R.id.me_linear);
        avator = (ImageView)view.findViewById(R.id.login_avator);
        nickName = (TextView)view.findViewById(R.id.login_nickName);
        view.findViewById(R.id.ll_collect).setOnClickListener(this);
        view.findViewById(R.id.ll_suggestion).setOnClickListener(this);
        view.findViewById(R.id.ll_guanyu).setOnClickListener(this);
        view.findViewById(R.id.ll_exit).setOnClickListener(this);
        tvLogin.setOnClickListener(this);
//        EventBus.getDefault().register(this);

    }

    @Override
    public void initView() {
        //初始化
        mShareAPI = UMShareAPI.get(getActivity());

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_mee;
    }

    //接口回掉结果
    private UMAuthListener umAuthListener = new UMAuthListener() {
        @Override
        public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
//            if (data.get("screen_name") != null ) {
//                Log.e("TAG", data.get("screen_name"));
//                final String name = data.get("screen_name");
//                final String url = data.get("profile_image_url");
//                Log.i("TAG",url);
//                getActivity().runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        nickName.setText(name);
//                        avator.setImageURI(Uri.parse(url));
//                    }
//                });
//            }
            Toast.makeText(getFActivity().getApplicationContext(), "授权成功" + data.toString(), Toast.LENGTH_SHORT).show();


        }

        @Override
        public void onError(SHARE_MEDIA platform, int action, Throwable t) {
            Toast.makeText(getFActivity().getApplicationContext(), "登录失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform, int action) {
            Toast.makeText(getFActivity().getApplicationContext(), "取消登录", Toast.LENGTH_SHORT).show();
        }
    };


//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void setUseMessage(BeanInfo info) {
//        //订阅后进行授权和获取用户信息
//        mShareAPI.doOauthVerify(getActivity(), SHARE_MEDIA.SINA, umAuthListener);
//        mShareAPI.getPlatformInfo(getActivity(), SHARE_MEDIA.SINA, umAuthListener);
//        //订阅获取的数据
//        Map<String, String> userMessage = info.getUserMessage();
//        String profile_image_url = userMessage.get("profile_image_url");
//        String screen_name = userMessage.get("screen_name");
//
//        if (profile_image_url != null && screen_name != null) {
//        }
//
//    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.me_linear://denglu
                Intent intent = new Intent(getFActivity(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_collect://收藏
                startActivity(new Intent(getFActivity(), CollectionActivity.class));
                break;
            case R.id.ll_suggestion://建议
                startActivity(new Intent(getFActivity(), SuggestActivity.class));
                break;
            case R.id.ll_guanyu://关于
                startActivity(new Intent(getFActivity(), BuyActivity.class));
                break;
            case R.id.ll_exit://退出
                nickName.setText("");
                break;
        }
    }

}
